package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.UserPaidTrackService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;
    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;
	@Autowired
	private UserPaidTrackService userPaidTrackService;
	@Autowired
	private UserPaidTrackMapper userPaidTrackMapper;

	@Override
	public UserInfoVo getUserInfoVoByUserId(Long userId) {
//		1.获取用户信息对象
		UserInfo userInfo = this.getById(userId);
//		2.创建userInfoVo对象
		UserInfoVo userInfoVo = new UserInfoVo();
//		3.属性拷贝给vo
		BeanUtils.copyProperties(userInfo,userInfoVo);
		return userInfoVo;
	}

	@Override
	public void updateUser(Long userId, UserInfoVo userInfoVo) {
//		1.新建要修改的数据包
		UserInfo userInfo = new UserInfo();
//		2.拷贝属性
		BeanUtils.copyProperties(userInfoVo,userInfo);
		userInfo.setId(userId);
//		3.更新
		this.updateById(userInfo);
	}

	@Override //查询用户购买过的声音
	public Map<Long, Integer> userIsPaidTrack(Long userId, Long albumId, List<Long> trackIdList) {
//		创建map集合，把需要付费购买的声音打包成（声音id，0）的map形式：
		Map<Long, Integer> map = trackIdList.stream().collect(Collectors.toMap(trackId -> trackId, trackId -> 0));
//		1.新建查询用户购买的专辑的查询器，查出用户是否购买过该id的专辑
		UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getUserId, userId).eq(UserPaidAlbum::getAlbumId, albumId));
//		判断该用户是否购买过这个专辑：
		if (null != userPaidAlbum) { //用户买过这个专辑，返回（声音id，1）的map
			return trackIdList.stream().collect(Collectors.toMap(trackId -> trackId, trackId -> 1));
		}
//		用户没买过这个专辑：
//		查询用户购买过的声音的集合列表
		List<UserPaidTrack> userPaidTrackList = userPaidTrackMapper.selectList(new LambdaQueryWrapper<UserPaidTrack>().eq(UserPaidTrack::getUserId, userId).in(UserPaidTrack::getTrackId, trackIdList));
//		这个列表有可能是空
		if (!CollectionUtils.isEmpty(userPaidTrackList)) { //如果用户购买过的声音列表是空
			for (UserPaidTrack userPaidTrack : userPaidTrackList) {
				map.put(userPaidTrack.getTrackId(),1);
			}
		}
//		购买过声音,把相关id的value设置为1（map不允许键重复，id键的新值会覆盖旧值）：
			return map;
		}
	}

